package com.cyl.XmlDemo.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionUnsubscribeEvent;

import com.cyl.XmlDemo.entity.RoomInfo;
import com.cyl.XmlDemo.entity.RoomList;
import com.cyl.XmlDemo.entity.UserInfo;
import com.cyl.XmlDemo.entity.UserList;

@Component
public class SessionUnsubscribeListener implements ApplicationListener<SessionUnsubscribeEvent> {
	
	private static final Logger logger = LoggerFactory.getLogger(SessionUnsubscribeListener.class);
	
	@Autowired
	private RoomList roomList;
	
	@Autowired
	private UserList userList;

	@Override
	public void onApplicationEvent(SessionUnsubscribeEvent event) {
		StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
		String uid = headerAccessor.getSessionAttributes().get("sessionId").toString();
		//移除现存用户列表的当前用户
		UserInfo userInfo = userList.getUser(uid);
		userList.delUser(uid);
		//移除房间中的当前用户
		RoomInfo roomInfo = roomList.getRoom(userInfo.getCurrentRoom());
		roomInfo.removeUser(uid);
		//TODO 发送用户离开信息
		logger.debug("收到取消订阅信号",roomInfo.getSize());
	}

}
